package com.lh0811.forge.pserv.uacs.server.adapter.controller;

import com.github.pagehelper.PageInfo;
import com.lh0811.forge.dependency.basic_web.constant.AuthConstant;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.dependency.security.client.component.annotation.HasUrl;
import com.lh0811.forge.pserv.uacs.api.component.data_scope.DataScope;
import com.lh0811.forge.pserv.uacs.api.param.*;
import com.lh0811.forge.pserv.uacs.api.vo.UacsDataRuleVo;
import com.lh0811.forge.pserv.uacs.api.vo.UacsPlateRoleQueryVo;
import com.lh0811.forge.pserv.uacs.api.vo.UacsTenantDepartmentPostQueryVo;
import com.lh0811.forge.pserv.uacs.server.service.TenantPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "权限中心-组织机构岗位管理")
@RestController
@RequestMapping("/forge/plate/v1/uacs/tenant_department_post")
public class OpmsTenantDepartmentPostController {

    @Resource
    private TenantPostService tenantPostService;

    // 岗位创建
    @HasUrl
    @PutMapping("/create")
    @Operation(summary = "岗位创建", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse tenantPostCreate(@RequestBody @Valid UacsTenantDepartmentPostCreateParam param) throws Exception {
        tenantPostService.tenantPostCreate(param);
        return ServerResponse.createBySuccess();
    }

    // 岗位删除
    @HasUrl
    @DeleteMapping("/delete/{postId}")
    @Operation(summary = "岗位删除", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse tenantPostDelete(@PathVariable(value = "postId") Long postId) throws Exception {
        tenantPostService.tenantPostDelete(postId);
        return ServerResponse.createBySuccess();
    }

    // 岗位修改
    @HasUrl
    @PostMapping("/update")
    @Operation(summary = "岗位修改", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse tenantPostUpdate(@RequestBody @Valid UacsTenantDepartmentPostUpdateParam param) throws Exception {
        tenantPostService.tenantPostUpdate(param);
        return ServerResponse.createBySuccess();
    }

    // 岗位查询(分页)
    @HasUrl
    @GetMapping("/query/page")
    @Operation(summary = "岗位查询（分页）", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    @DataScope
    public ServerResponse<PageInfo<UacsTenantDepartmentPostQueryVo>> tenantPostQueryPage(@ParameterObject @Valid UacsTenantDepartmentPostQueryPageParam param) throws Exception {
        return ServerResponse.createBySuccess(tenantPostService.tenantPostQueryPage(param));
    }

    // 岗位查询(不分页)
    @HasUrl
    @GetMapping("/query/list")
    @Operation(summary = "岗位查询（不分页）", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    @DataScope
    public ServerResponse<List<UacsTenantDepartmentPostQueryVo>> tenantPostQueryList(@ParameterObject @Valid UacsTenantDepartmentPostQueryListParam param) throws Exception {
        return ServerResponse.createBySuccess(tenantPostService.tenantPostQueryList(param));
    }

    // 岗位角色关系保存
    @HasUrl
    @PostMapping("/role_ref/save")
    @Operation(summary = "岗位角色关系保存", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse tenantPostRoleRefSave(@RequestBody @Valid UacsTenantPostRoleRefSaveParam param) throws Exception {
        tenantPostService.tenantPostRoleRefSave(param);
        return ServerResponse.createBySuccess();
    }

    // 岗位数据规则保存
    @HasUrl
    @PostMapping("/data_rule_ref/save")
    @Operation(summary = "岗位数据规则保存", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse tenantPostDataRuleSave(@RequestBody @Valid UacsTenantPostDataRuleRefSaveParam param) throws Exception {
        tenantPostService.tenantPostDataRuleSave(param);
        return ServerResponse.createBySuccess();
    }


    // 获取岗位绑定的角色列表
    @HasUrl
    @GetMapping("/building/role_list/{postId}")
    @Operation(summary = "获取岗位绑定的角色列表", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<List<UacsPlateRoleQueryVo>> getPostBuildingRoleList(@PathVariable(value = "postId") Long postId) throws Exception {
        return ServerResponse.createBySuccess(tenantPostService.getPostBuildingRoleList(postId));
    }

    // 获取岗位绑定的数据规则列表
    @HasUrl
    @GetMapping("/building/data_rule_list/{postId}")
    @Operation(summary = "获取岗位绑定的数据规则列表", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse<List<UacsDataRuleVo>> getPostBuildingDataRuleList(@PathVariable(value = "postId") Long postId) throws Exception {
        return ServerResponse.createBySuccess(tenantPostService.getPostBuildingDataRuleList(postId));
    }

}
